<?php
require_once ("db.inc");
require_once ("math.inc");
require_once ("xajax/xajax_core/xajax.inc.php");

define ("domain","http://путя.рф/");
define ("empty_link_text","Вставьте сюда вашу ссылку...");
$xajax = new xajax();
$xajax->registerFunction("get_short_link");

function get_short_link ($arg){
    // проверить ссылка ли это!!!
    $objResponse = new xajaxResponse();

    if ($arg == empty_link_text) {
        $objResponse->assign("txtshort", "value", "Пусто :(");
    }
    else if (mb_strpos($arg,"путя.рф")) {
        $objResponse->assign("txtshort", "value", "Ну ну ;D");
    }    
    else if (preg_match("#^(http|https|ftp)\://#", $arg)) {
        if ( $id = getmysqli_query ("SELECT `simple`.`id` FROM `putya`.`simple` WHERE `simple`.`link` = '".$arg."'")) {
            $short = get76from10($id[0]['id']);
            $objResponse->assign("txtshort", "value", domain.$short);
        }
        else
        {
            if ( $identity = getmysqli_query ("INSERT INTO `putya`.`simple` (`id` ,`link`) VALUES ( NULL , '".$arg."');"))
            {
                $code76 = get76from10($identity);
                $objResponse->assign("txtshort", "value", domain.$code76);
            }
        }
    }
    else
        $objResponse->assign("txtshort", "value", "Ты ввел белиберду ;)");

    return $objResponse;
}


$xajax->processRequest();

$free_url="Что сократить?!";

$uri = substr($_SERVER[REQUEST_URI],1);
//print_r($_SERVER);
if (!isset($_GET['getlink']) && !empty($uri))
//if (!isset($_GET['getlink']) && !empty($uri) && $_SERVER['REMOTE_ADDR'] == '213.170.92.14')
{
    // поиск по базе
    $uri = urldecode($uri);    
    if (preg_match("/^[а-яА-Я0-9]{1,10}+$/u", $uri)) {
        
        $code10 = get10from76($uri);
        if ( $result = getmysqli_query ("SELECT `link` FROM `simple` WHERE `id` = '".$code10."'")) {
            getmysqli_query ("INSERT INTO `putya`.`statistic` (`id` ,`date`,`linkid`,`comments`) VALUES ( NULL , CURDATE(),'".$code10."','".$_SERVER['REMOTE_ADDR']."');");
            $result = $result[0]['link'];
            header ("Location: ".$result."");
        }
        else
        {
            $free_url = domain.$uri;
        }

    }
}

?>
<!DOCTYPE html>
<html lang="ru">
<head>

    <meta charset="utf-8" />

    <!-- Always force latest IE rendering engine (even in intranet)
         & Chrome Frame -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

    <title>ПУТЯ.РФ - Сокращатель ссылок по-русски</title>

    <meta name="description" content="путя.рф - сокращаем ссылки">
    <meta name="keywords" content="путя, рф, короткие, кириллица, русский, по-русски, национальный, 
          сократить, сокращатель, сокращаем, линк, ссылка, ссылки, ссылок,
          ссылочка, клик, твиттер, фейсбук, вконтакте, живой, журнал">
    <meta name="copyright" content="WinkMe, Copyright 2010 - winkme@ctoh.net">
    <meta name="Address" content="Moscow, Saint-Petersburg, Russia" >
    <meta name="Author" content="WinkMe, winkme@ctoh.net">
    <meta name="Design" content="Kripta, kripta@ctoh.net">
    <meta HTTP-EQUIV="Expires" content="Mon, 11 Jun 1998 00:00:01 GMT">
    <meta name="revisit-after" content="10 days">
    <!-- <meta HTTP-EQUIV="pragma" content="no-cache"> -->
    <meta HTTP-EQUIV="Page-Enter" content="BlendTrans(Duration=2.0)">
    <meta HTTP-EQUIV="Page-Exit" content="BlendTrans(Duration=2.0)">

    <link href="css/960.css" rel="stylesheet" type="text/css" />
<?php if ( date('n') >= 4 && date('n') <= 9 ) {?>  
    <link href="style-summer.css" rel="stylesheet" type="text/css" />
<?php }
      else { ?>  
    <link href="style-winter.css" rel="stylesheet" type="text/css" />
<?php } ?>
    
    <link rel="shortcut icon" type="text/css" href="favicon.png"></link>
    <!--[if lt IE 9]>
    	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"
                type="text/javascript"></script>
    <![endif]-->

    <!--[if lte IE 7]>
    	<link href="css/ie.css" rel="stylesheet" type="text/css" />
    <![endif]-->

    <script src="http://vkontakte.ru/js/common.js"></script>
    <script src="openapi/openapi_sample.js"></script>

    <script type="text/javascript"
            src="zeroclipboard/ZeroClipboard.js"></script>

    <script language="JavaScript">
            var clip = null;
            
            function init() {
                clip = new ZeroClipboard.Client();
                clip.setHandCursor( true );

                clip.glue( 'd_clip_button', 'd_clip_container' );

                clip.addEventListener( 'mouseDown', function(client) {
                    // set text to copy here
                    clip.setText( document.getElementById('txtshort').value );

                } );
            }
    </script>
<?php $xajax->printJavascript("xajax/");?>
</head>

<body id="" onLoad="init()">



    <script type="text/javascript" src="//yandex.st/share/share.js" charset="utf-8"></script>
    
    <div id="divheader">
    </div>

    <div align="center" id="share" >
        <div class="yashare-auto-init" data-yashareL10n="ru" data-yashareType="button" data-yashareQuickServices="yaru,vkontakte,facebook,twitter,odnoklassniki,moimir,moikrug,myspace"></div> 
    </div>     
    
    <div id="divcontent">
        <div id="div-left-side-panel">
            <div id="divlink">

                <input type   ="text"
                       id     ="txtlink"
                       value  ="<?php
                           if ( !isset($_GET['getlink']) )
                               { echo empty_link_text; }
                           else { echo $_GET['getlink']; } ?>"
                       onclick="if (this.value=='<?php echo empty_link_text ?>')
                                    { this.value='' }"
                       onblur ="if (this.value=='')
                                    { this.value='<?php echo empty_link_text ?>' }"
                       onkeyup="if (event.keyCode == 13) {
                                    xajax_get_short_link(this.value)
                                }"
                />

            </div>
            <div id="divshort">

                <input type="text" id="txtshort" readonly
                       value="<?php echo $free_url ?>" />

<?php /*if (!strpos($_SERVER['HTTP_USER_AGENT'],"MSIE")) { */ ?>

                <div id="d_clip_container">
                    <div id="d_clip_button" class="my_clip_button">Копируем</div>
                </div>

<?php /*}*/ ?>
                
            </div>   
        </div>

        <div id="divbutton">


            <input id="btngetshort" type="button" 
                   onclick="xajax_get_short_link(document.getElementById('txtlink').value)"/>
            <!--
            <button id="btngetshort"
                    onclick="xajax_get_short_link(document.getElementById('txtlink').value)"
            />
            -->

        </div>
    </div>

    <div id="divfooter">
        <p id="copyright">&copy; 2011 ПУТЯ.РФ</p>
    </div>

    <div id="dropbookmarks">
        <a href="javascript:location.href='<?php echo domain ?>?getlink='+escape(location.href)">
            Сократить с Путя.РФ!
        </a><b><br/>тащи в закладки</b>
    </div>       
    
<!-- Yandex.Metrika counter -->
<div style="display:none;"><script type="text/javascript">
(function(w, c) {
    (w[c] = w[c] || []).push(function() {
        try {
            w.yaCounter3294052 = new Ya.Metrika({id:3294052});
        }
        catch(e) { }
    });
})(window, 'yandex_metrika_callbacks');
</script></div>
<script src="//mc.yandex.ru/metrika/watch.js" type="text/javascript" defer="defer"></script>
<noscript><div><img src="//mc.yandex.ru/watch/3294052" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->

</body>
</html>